import { decryptRes, dealPagination } from '../utils/requestUtil';
import * as orderService from '../services/orderService';
import * as franService from '../services/franService';
import { message } from 'antd';

export default {
  namespace: 'order',

  state: {
    orderList: [],
    pagination: {},
    baseInfo: {},
    tradeInfo: {},
    sendOutGoodsInfo: {},
    refundsInfo: {},
    exportOrderList: [],

    franList: [],
  },

  effects: {
    *fetchOrderList({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(orderService.fetchOrderList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'orderListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchFranList({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(franService.fetchFranList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'franListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchOrderDetail({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(orderService.fetchOrderDetail, payload)));
      console.log('订单详情----------',response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'orderDetailReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *changeOrderStatus({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(orderService.changeOrderStatus, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;

        message.success('操作成功！')

        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *exportOrder({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(orderService.exportOrder, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;

        message.success('删除成功！')

        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *closeOrder({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(orderService.closeOrder, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;

        message.success('操作成功！')

        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *sendOrder({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(orderService.sendOrder, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;

        message.success('操作成功！')

        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },


    *refundOrder({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(orderService.refundOrder, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;

        message.success('操作成功！')

        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
  },

  reducers: {
    orderListReducer(state, action) {
      return {
        ...state,
        orderList: action.payload.data.dataList,
        pagination: dealPagination(action.payload.data.pageInfo),
      };
    },

    orderDetailReducer(state, action) {
      console.log('couponDetailReducercouponDetailReducer??', action.payload.data.baseInfo)
      return {
        ...state,
        baseInfo: action.payload.data.baseInfo,
        tradeInfo: action.payload.data.tradeInfo,
        sendOutGoodsInfo: action.payload.data.sendOutGoodsInfo,
        refundsInfo: action.payload.data.refundsInfo,
      };
    },

    franListReducer(state, action) {
      return {
        ...state,
        franList: action.payload.data.dataList,
      };
    },
  },
};
